package org.bite.lesson05;

import java.util.concurrent.TimeUnit;

public class Demo_503 {
    public static void main(String[] args) {
        //定义一个锁对象
        Object locker = new Object();
        //创建调用wait的线程
        Thread t1 = new Thread(()->{
            while(true){
                synchronized(locker){
                    System.out.println("调用wait()之前...");
                    //线程要执行的逻辑
                    //如果没要满足线程所需的数据就等待
                    try{
                        locker.wait();
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                    System.out.println("wait()唤醒之后..");
                    System.out.println("===================");
                }
            }
        });

        Thread t2 = new Thread(() -> {
            while (true) {
                System.out.println("notifyAll()之前...");
                // 同等待的锁对象进行唤醒
                synchronized (locker) {
                    locker.notifyAll();
                }

                System.out.println("notifyAll()之后...");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        // 启动线程
        t1.start();
        t2.start();
    }
}
